var time = 0;
//游戏全局控制脚本
cc.Class({
    extends: cc.Component,

    properties: {
        //背景滚动速度
        Bg_Speed: 0,
        //地面滚动速度
        Gd_Speed: 0,
        //障碍滚动速度
        Pe_Speed: 0,
        //小鸟上升距离
        Bird_Lift: 0,
        //小鸟下落距离
        Bird_Drop: 0,
        //当前小鸟到达地面的y轴位置
        Drop_Ground: 0,
        //当前小鸟到达顶部的y轴位置
        Up_Sky: 0,
        //小鸟对象
        Bird: {
            default: null,
            type: cc.Node
        },
        //障碍管理节点
        Pipe: {
            default: null,
            type: cc.Node
        },
        //计分组件
        Label_Score: {
            default: null,
            type: cc.Label
        },
        //UI总结点
        Total_Ui: {
            default: null,
            type: cc.Node
        }
    },

    onLoad () {
        //将当前脚本付给全局变量
        Game.Self = this;
        Game.PipeManager = this.Pipe.getComponent("PipeControl");
        Game.UiNode = this.Total_Ui;
        this.AudioSource = this.node.getComponent(cc.AudioSource);
        this.Listener_Click(this.node.parent);
        // this.schedule(function(){
        //     this.Control_Rotation(6,2,90);
        // },0.195);
    },

    update (dt) {
        if((Game.End == true && Game.BirdState == "Init") 
            || (Game.End == true && Game.BirdState == "Ground")) {return;}
        this.Control_Drop(this.Bird_Drop,0.0488);
        this.Update_Score();
    },

    //点击时间监听
    Listener_Click (node) {
        node.on(cc.Node.EventType.TOUCH_START,this.Control_Bird);
    },

    //小鸟上升控制函数
    Control_Bird (event) {
        var bird_pos = 0;
        var game = Game.Self;
        var bird = Game.Self.Bird;
        var lift = Game.Self.Bird_Lift;
        bird_pos = bird.y;
        //若当前游戏未开始则禁用上升
        if(Game.End == true) {
            Game.Reset = true;
            return;
        }
        //判断小鸟是否撞到顶部或者上方障碍
        if(bird_pos > game.Up_Sky || (Game.UpTag == true && bird_pos > Game.UpNum)) {
            var boundary = (bird_pos > game.Up_Sky) ? boundary = game.Up_Sky : Game.UpNum;
            game.y = boundary;
            Game.Self.AudioSource.play();
            return;
        }
        time = 0;
        bird.x = 0;
        bird.y += lift;
        bird.rotation = 0;
        Game.Self.AudioSource.play();
    },

    //小鸟下降函数
    Control_Drop (speed,int) {
        var g = 9.8;
        var t = time * time;
        var s = (g * t) / 2;
        var bird_pos = this.Bird.y - s.toFixed(4);
        time += int;
        Game.Self.Bird.x = 0;
        if(bird_pos < this.Drop_Ground || (Game.DropTag == true && bird_pos < Game.DropNum)) {
            var boundary = (bird_pos < this.Drop_Ground) ? boundary = this.Drop_Ground : Game.DropNum; 
            Game.End = true;
            Game.Reset = true;
            Game.BirdState = "Ground";
            this.Bird.y = boundary;
            time = 0;
            return;
        }
        this.Bird.y -= s.toFixed(4);
        
    },

    //小鸟旋转
    //[frame:帧率 speed:速度 degree:总旋转角度]
    //[旋转速率 = degree / (1 / frame) * speed]
    Control_Rotation (frame,speed,degree) {
        if(Game.End == true) {return;}
        var Rs = degree * ((1 / frame) * speed);
        Rs = parseFloat(Rs.toFixed(4));
        if(parseInt(this.Bird.rotation) == degree) {
            return;
        }
        this.Bird.rotation += Rs;
    },

    //更新分数
    Update_Score () {
        this.Label_Score.string = Game.Score;
    },

    //重置游戏
    Reset_Game () {
        this.Bird.x = 0;
        this.Bird.y = 22;
        this.Bird.rotation = 0;
        Game.Score = 0;
        Game.DropTag = false,  //重置下降障碍标记【表示当前小鸟碰到的下方障碍】
        Game.UpTag = false,  //重置上升障碍标记【表示当前小鸟碰到的上方障碍】
        Game.DropNum = 0,  //重置当前下方障碍的Y轴最高点
        Game.UpNum = 0, //重置当前上方障碍的Y轴最低点
        Game.PipeManager.Reset_Position();
    }

});
